package com.link.base.base.product.service;

import com.link.base.base.cmqres.model.CmqRes;
import com.link.base.base.cmqres.service.CmqResService;
import com.link.base.base.product.dao.mybatis.mapper.PriceListItemMapper;
import com.link.base.base.product.model.PriceList;
import com.link.base.base.product.model.PriceListItem;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.mq.util.MqUtil;
import com.link.core.util.DateUtil;
import com.link.core.util.StringUtils;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author unknown
 * @date unknown
 * @comments 价格表行项目服务
 */
@Service
public class PriceListItemServiceImpl extends BasicServiceImpl<PriceListItem> implements PriceListItemService {

    @Resource
    private PriceListItemMapper priceListItemMapper;


    @Resource
    private CmqResService cmqResService;

    @Resource
    private PriceListService priceListService;

    @Override
    public BasicMapper<PriceListItem> getBasicMapper() {
        return priceListItemMapper;
    }

    /**
     * 根据产品ID和价格表ID查找价格表行项目
     *
     * @param entity
     * @return 价格表信息
     * @throws Exception
     */
    @Override
    public List<PriceListItem> queryByProdId(PriceListItem entity) throws Exception {
        return priceListItemMapper.queryByProdId(entity);
    }

    @Override
    public void afterUpdate(PriceListItem priceListItem) throws Exception {
        CmqRes cmqRes = new CmqRes();
        List<CmqRes> cmqResList = cmqResService.queryAll(cmqRes);
        if (cmqResList.size() > 0) {
            try {
                Long accntId = priceListItem.getHeadId();
                Long itemId = priceListItem.getId();
                if (accntId != null) {
                    PriceList priceListQuery = new PriceList();
                    priceListQuery.setId(accntId);
                    PriceList priceList = priceListService.queryById(priceListQuery);
                    if (priceList == null) {
                        LCLogger.error().withMessageKey("afterUpdate")
                                .withMessage("查询到表头为空！").flush();
                        return;
                    }
//                  String isReadOnly = priceList.getIsReadOnly();
//                  if ("Y".equals(isReadOnly)) {
//                      return;
//                  }

                    String message1 = "PriceTableSyncl;{\"action\":\"Update\",\"objectId\":\"";
                    String message2 = "\"}";
                    String message = message1 + accntId + ";" + itemId + message2;
                    String topicName = cmqResList.get(0).getQueueName();
                    MqUtil.sendCmqTopic(topicName, message);
                }
            } catch (Exception e) {
                LCLogger.withException(e);
            }
        }
    }

    @Override
    public void afterInsert(PriceListItem priceListItem) throws Exception {
        CmqRes cmqRes = new CmqRes();
        List<CmqRes> cmqResList = cmqResService.queryAll(cmqRes);
        if (cmqResList.size() > 0) {
            try {
                Long accntId = priceListItem.getHeadId();
                Long itemId = priceListItem.getId();
                if (accntId != null) {
                    PriceList priceListQuery = new PriceList();
                    priceListQuery.setId(accntId);
                    PriceList priceList = priceListService.queryById(priceListQuery);
                    if (priceList == null) {
                        LCLogger.error().withMessageKey("afterInsert")
                                .withMessage("查询到的表头为空！").flush();
                        return;
                    }
//                  String isReadOnly = priceList.getIsReadOnly();
//                  if ("Y".equals(isReadOnly)) {
//                      return;
//                  }
                    String message1 = "PriceTableSyncl;{\"action\":\"Update\",\"objectId\":\"";
                    String message2 = "\"}";
                    String message = message1 + accntId + ";" + itemId + message2;
                    String topicName = cmqResList.get(0).getQueueName();
                    MqUtil.sendCmqTopic(topicName, message);
                }
            } catch (Exception e) {
                LCLogger.withException(e);
            }
        }
    }

    @Override
    public void onOrOffPriceItem(PriceListItem entity) throws Exception {
        if (entity.getId() == null) {
            throw new ServiceException("PUBLIC-007");
        }
        //有效开始时间
        String effStartDate = null;
        //有效结束时间
        String effEndDate = null;
        //状态
        String priceStatus = entity.getPriceStatus();

        PriceListItem priceItem = queryById(entity);
        //上架
        String onSale = "OnSale";
        String notSale = "NotSale";
        if (StringUtils.equals(onSale, priceStatus)) {
            Double promoPrice = priceItem.getPromoPrice();
            if (promoPrice == null || promoPrice <= 0) {
                throw new ServiceException("PRODUCT-007");
            }
            //获取1小时前的时间，降低系统时间不准确的风险
            Date tempDate = DateUtil.addTimes(Calendar.HOUR, new Date(), -1);
            effStartDate = DateUtil.dateToStr(tempDate, "yyyy-MM-dd HH:mm:ss");
            effEndDate = null;
        } else if (StringUtils.equals(notSale, priceStatus)) {
            //下架
            //获取1小时前的时间，降低系统时间不准确的风险
            Date tempDate = DateUtil.addTimes(Calendar.HOUR, new Date(), -1);
            Date tempDate1 = DateUtil.addTimes(Calendar.DATE, new Date(), -1);
            effStartDate = DateUtil.dateToStr(tempDate1, "yyyy-MM-dd HH:mm:ss");
            effEndDate = DateUtil.dateToStr(tempDate, "yyyy-MM-dd HH:mm:ss");
        } else {
            throw new ServiceException("PRODUCT-008");
        }
        priceItem.setEffStartDate(effStartDate);
        priceItem.setEffEndDate(effEndDate);
        update(priceItem);
    }
}
